import dao.AdminDao;
import dao.DepartmentDao;
import dao.EmployeeDao;
import dao.impl.AdminDaoImpl;
import dao.impl.DepartmentImpl;
import dao.impl.EmployeeImpl;
import pojo.Admin;
import pojo.Department;
import pojo.Employee;
import view.EmployeeView;

import java.util.List;
import java.util.Scanner;
import java.util.regex.Pattern;

public class Main {
    private static Scanner sc = new Scanner(System.in);
    static Admin admin = null;
    public static void main(String[] args) {
        EmployeeDao employeeDao = new EmployeeImpl();
        DepartmentDao departmentDao = new DepartmentImpl();

        AdminDao adminDao = new AdminDaoImpl();

        String choice;
        do{
            printMainUi();
            choice = sc.next();
            switch(choice){
                case "0":
                    if(admin == null){
                        System.out.print("请输入用户名: ");
                        String user = sc.next();
                        System.out.print("请输入密码: ");
                        String passwd = sc.next();
                        admin = adminDao.login(user,passwd);
                        if(admin == null){
                            System.out.println("用户名或密码错误");
                        }else{
                            System.out.println("登陆成功");
                        }
                    }else{
                        admin = null;
                    }
                    break;
                case "1":
                    if(admin == null){
                        System.out.println("您还未登陆,请先登陆");
                        break;
                    }
                    System.out.println("===========当前正在添加员工===========");
                    System.out.print("输入员工姓名: ");
                    String name = sc.next();
                    System.out.print("输入员工性别: ");
                    String sex = sc.next();
                    System.out.print("输入员工年龄: ");
                    int age = sc.nextInt();
                    System.out.print("输入员工月薪: ");
                    double salary = sc.nextDouble();
                    System.out.print("输入部门id: ");
                    int depId = sc.nextInt();
                    Department dep = departmentDao.selectDepartment(depId);
                    if(dep != null){
                        Employee emp = new Employee(name,sex,age,salary,dep);
                        employeeDao.addEmployee(emp);
                        System.out.println("添加员工成功!");
                    } else{
                        System.out.println("添加失败,未找到部门!");
                    }
                    break;
                case "2":
                    if(admin == null){
                        System.out.println("您还未登陆,请先登陆");
                        break;
                    }
                    System.out.println("===========当前正在删除员工===========");
                    System.out.print("输入要删除的员工ID: ");
                    int delId = sc.nextInt();
                    Employee delEmployee = employeeDao.selectEmployee(delId);
                    if(delEmployee != null){
                        employeeDao.delEmployee(delId);
                        System.out.println("删除成功!");
                    }else{
                        System.out.println("未找到该员工!");
                    }
                    break;
                case "3":
                    if(admin == null){
                        System.out.println("您还未登陆,请先登陆");
                        break;
                    }
                    System.out.println("+----------------------------------------------------+");
                    System.out.println("                      [员工查询]                       ");
                    System.out.println("    1.按照id查询      2.按照月薪查询      3.按照部门查询    ");
                    System.out.println("+----------------------------------------------------+");
                    System.out.print("请输入查询序号: ");
                    String choice1 = sc.next();
                    switch(choice1){
                        case "1":
                            System.out.print("请输入要查询的ID: ");
                            int id = sc.nextInt();
                            Employee emp1 = employeeDao.selectEmployee(id);
                            if(emp1 != null){
                                EmployeeView.printEmployee(emp1);
                            }else{
                                System.out.println("未找到该员工!");
                            }
                            break;
                        case "2":
                            System.out.print("请输入查询的月薪条件(例如 >5000, <=6000): ");
                            String str = sc.next();
                            Pattern pattern = Pattern.compile("^(([><=]|[><]=)\\d+)$");
                            boolean isMatch = pattern.matcher(str).find();
                            if(isMatch){
                                List<Employee> list = employeeDao.selectEmployeeBySalary(str);
                                EmployeeView.printAllEmployee(list);
                            }else{
                                System.out.println("输入格式有误!");
                            }
                            break;
                        case "3":
                            System.out.print("请输入要查询的部门ID: ");
                            int dId = sc.nextInt();
                            EmployeeView.printAllEmployee(employeeDao.selectEmployeeByDep(dId));
                            break;
                    }
                    break;
                case "4":
                    if(admin == null){
                        System.out.println("您还未登陆,请先登陆");
                        break;
                    }
                    System.out.println("===========当前正在修改员工===========");
                    System.out.print("输入要修改的员工ID: ");
                    int updateId = sc.nextInt();
                    Employee updateEmp = employeeDao.selectEmployee(updateId);
                    if(updateEmp != null) {
                        System.out.print("修改员工姓名: ");
                        String updateName = sc.next();
                        System.out.print("修改员工性别: ");
                        String updateSex = sc.next();
                        System.out.print("修改员工年龄: ");
                        int updateAge = sc.nextInt();
                        System.out.print("修改员工月薪: ");
                        double updateSalary = sc.nextDouble();
                        System.out.print("修改部门id: ");
                        int UpdateDepId = sc.nextInt();
                        Department updateDep = departmentDao.selectDepartment(UpdateDepId);
                        if(updateDep != null){
                            employeeDao.updateEmployee(updateId,updateName,updateSex,updateAge,updateSalary,updateDep);
                            System.out.println("修改成功!");
                        }else{
                            System.out.println("修改失败,部门id不存在!");
                        }
                    }else{
                        System.out.println("未找到该员工!");
                    }
                    break;
                case "5":
                    employeeDao.showAllEmployee();
                    break;
                case "6":
                    if(admin == null){
                        System.out.println("您还未登陆,请先登陆");
                        break;
                    }
                    System.out.println("+------------------------------------------------------------+");
                    System.out.println("                           [部门管理]                          ");
                    System.out.println("    1.添加部门      2.删除部门      3.修改部门     4.查询所有部门    ");
                    System.out.println("+------------------------------------------------------------+");
                    System.out.print("请输入查询序号: ");
                    String choice2 = sc.next();
                    switch(choice2){
                        case "1":
                            System.out.println("===========当前正在添加部门===========");
                            System.out.print("输入部门的ID: ");
                            int id = sc.nextInt();
                            if(departmentDao.selectDepartment(id) == null){
                                System.out.print("输入部门的名称: ");
                                String depName = sc.next();
                                departmentDao.addDepartment(new Department(id,depName));
                                System.out.println("添加成功!");
                            }else{
                                System.out.println("该ID已存在!请勿重复添加!");
                            }
                            break;
                        case "2":
                            System.out.println("===========当前正在删除部门===========");
                            System.out.print("输入要删除的部门ID: ");
                            int delDepId = sc.nextInt();
                            Department delDepartment = departmentDao.selectDepartment(delDepId);
                            if(delDepartment != null){
                                departmentDao.delDepartment(delDepId);
                                System.out.println("删除成功!");
                            }else{
                                System.out.println("未找到该部门!");
                            }
                            break;
                        case "3":
                            System.out.println("===========当前正在修改部门===========");
                            System.out.print("请输入要修改的部门ID: ");
                            int dId = sc.nextInt();
                            if(departmentDao.selectDepartment(dId) != null){
                                System.out.print("请输入修改的部门ID: ");
                                int newId = sc.nextInt();
                                System.out.print("请输入修改的部门名称: ");
                                String newName = sc.next();
                                departmentDao.updateDepartment(dId,newId,newName);
                                System.out.println("修改成功!");
                            }else{
                                System.out.println("未查询到部门!");
                            }
                            break;
                        case "4":
                            departmentDao.showALLDepartment();
                            break;
                    }
                    break;
                case "9":
                    System.out.println("============当前正在添加管理员============");
                    System.out.print("输入用户名: ");
                    String adName = sc.next();
                    System.out.print("输入密码: ");
                    String pass = sc.next();
                    System.out.print("再次输入密码: ");
                    String rePass = sc.next();
                    if(pass.equals(rePass)){
                        adminDao.register(adName,pass);
                        System.out.println("添加成功");
                    }else{
                        System.out.println("两次密码不一致!");
                    }
                    break;
            }
        }while(!choice.equals("7"));
    }

    public static void printMainUi(){
        String login = null;
        if(admin == null){
            login = "管理员登陆";
        }else{
            login = "退出登陆";
        }
        System.out.println("+-----------------------------------------------------------+");
        System.out.println("                        [员工管理系统]                         ");
        System.out.println("     1.添加员工      2.删除员工      3.查询员工      4.修改员工    ");
        System.out.println("     5.显示所有员工   6.部门管理      7.退出系统                  ");
        System.out.println("                        0."+login+"                          ");
        System.out.println("+-----------------------------------------------------------+");
        System.out.print("请输入: ");
    }
}